using System;
using System.Diagnostics.Contracts;
using System.Linq.Expressions;
using Charisma.Linq;
using Charisma.MongoDb.Linq.Expressions;

namespace Charisma.MongoDb.Linq
{
  internal sealed class TranslatorContext
  {
    public Expression Query { get; private set; }
    public ExpressionEvaluator Evaluator { get; private set; }
    public ParameterExtractor ParameterExtractor { get; private set; }
    public BindingCollection<ParameterExpression, ProjectionExpression> Bindings { get; private set; }

    public bool IsRoot(Expression expression)
    {
      return expression == Query;
    }

    [ContractInvariantMethod]
    private void ObjectInvariant()
    {
      Contract.Invariant(Query != null);
    }


    public TranslatorContext(Expression root)
    {
      Contract.Requires(root != null);

      Query = root;
      Evaluator = new ExpressionEvaluator(root);
      ParameterExtractor = new ParameterExtractor(Evaluator);
      Bindings = new BindingCollection<ParameterExpression, ProjectionExpression>();
    }
  }
}